package com.yeswater.multithread.chapter7;

import org.junit.Test;

/**
 * author： yeswater
 * create： 2023/11/8
 *
 * 线程异常处理的优先性:
 *      结论：
 *          如果调用 setUncaughtExceptionHandler(),这其设置的异常处理器优先运行，其他的异常处理器不运行！！
 */
public class Run_7_9_1 {
    public static void main(String[] args) {
    
    }
    
    /**
     * 对象的异常处理
     * java.lang.NumberFormatException: For input string: "a"
     */
    @Test
    public void test1() {
        Thread1 t1 = new Thread1();
        t1.setUncaughtExceptionHandler(new ObjectUncaughtExceptionHandler());
        Thread1.setDefaultUncaughtExceptionHandler(new StateUncaughtExceptionHandler());
        t1.start();
    }
    
    /**
     * 静态的异常处理
     * java.lang.NumberFormatException: For input string: "a"
     * 	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
     * 	at java.base/java.lang.Integer.parseInt(Integer.java:668)
     */
    @Test
    public void test2() {
        Thread1 t1 = new Thread1();
        //t1.setUncaughtExceptionHandler(new ObjectUncaughtExceptionHandler());
        Thread1.setDefaultUncaughtExceptionHandler(new StateUncaughtExceptionHandler());
        t1.start();
    }
    
    /**
     * 对象的异常处理
     * java.lang.NumberFormatException: For input string: "a"
     */
    @Test
    public void test3() {
        ThreadGroup1 group = new ThreadGroup1("我的线程组");
        Thread1 t1 = new Thread1(group, "我的线程");
        t1.setUncaughtExceptionHandler(new ObjectUncaughtExceptionHandler());
        Thread1.setDefaultUncaughtExceptionHandler(new StateUncaughtExceptionHandler());
        t1.start();
    }
    
    /**
     * 静态的异常处理
     * java.lang.NumberFormatException: For input string: "a"
     */
    @Test
    public void test4() {
        ThreadGroup1 group = new ThreadGroup1("我的线程组");
        Thread1 t1 = new Thread1(group, "我的线程");
        //t1.setUncaughtExceptionHandler(new ObjectUncaughtExceptionHandler());
        Thread1.setDefaultUncaughtExceptionHandler(new StateUncaughtExceptionHandler());
        t1.start();
    }
    
    private static class Thread1 extends Thread {
        String num = "a";
        
        public Thread1() {
            super();
        }
    
        public Thread1(ThreadGroup group, String name) {
            super(group, name);
        }
        
        @Override
        public void run() {
            int numInt = Integer.parseInt(num);
            System.out.println("在线程中打印：" + (numInt + 1));
        }
    }
    
    private static class ThreadGroup1 extends ThreadGroup {
        public ThreadGroup1(String name) {
            super(name);
        }
    
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            super.uncaughtException(t, e);
            System.out.println("线程组的异常处理");
            e.printStackTrace();
        }
    }
    
    private static class ObjectUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("对象的异常处理");
            e.printStackTrace();
        }
    }
    
    private static class StateUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("静态的异常处理");
            e.printStackTrace();
        }
    }
}
